import sys
import os
import base64
import json

path = 'bazel-bin/cc/appcoin2/support/order'
sys.path.append(path)

import py_trade_client

query = {
    # "mea": "Spot.Binance.v1",
    # "mea": "Futures.Binance.v1",
    # "mea": "Spot.Bitbank.v1",
    # "mea": "Spot.Bitflyer.v1",
    # "mea": "Futures.Bitflyer.v1",
    # "mea": "Spot.Bithumb.v2",
    # "mea": "Futures.Bitmex.v1",
    # "mea": "Spot.Bitstamp.v2",
    # "mea": "Futures.Bybit.v2",
    # "mea": "Spot.Coinone.v2",
    # "mea": "Spot.Ftx.v1",
    # "mea": "Futures.Ftx.v1",
    # "mea": "Spot.Gdax.v1",
    # "mea": "Spot.Gopax.v1",
    # BUG "mea": "Spot.Huobi.v1",
    # "mea": "Futures.Huobi.v1",
    # BUG "mea": "Spot.Korbit.v1",
    # "mea": "Spot.Kraken.v1",
    # "mea": "Spot.Lmaxdigital.v1",
    # "mea": "Spot.Okex.v3",
    # "mea": "Futures.Okex.v3",
    # "mea": "Spot.Okex.v5",
    # "mea": "Futures.Okex.v5",
    # "mea": "Futures.Prex.v1",
    # ERR "mea": "Spot.Upbit.v1",
    "mea": "Futures.Deribit.v2",
    "type": "QUERY_ACCOUNT_BALANCE",
    # "exchange_order_id": "4de1275b-cdc6-0d4c-5c82-1f06757b5020"
}

# key_path = '~/workspace/xunke00/binance/view_key.json'
# key_path = '~/workspace/xunke00/binance_fut/view_key.json'
# key_path = '~/workspace/xunke00/bitbank/view_key.json'
# key_path = '~/workspace/xunke05/bitflyer/view_key.json'
# key_path = '~/workspace/xunke05/bitflyer/view_key.json'
# key_path = '~/workspace/xunke00/bithumb/view_key.json'
# key_path = '~/workspace/xunke00/bitmex/view_key.json'
# key_path = '~/workspace/xunke00/bitstamp/view_key.json'
# key_path = '~/workspace/xunke00/bybit/view_key.json'
# key_path = '~/workspace/xunke00/coinone/view_key.json'
# key_path = '~/workspace/xunke00/FTX/view_key.json'
# key_path = '~/workspace/xunke00/FTX/view_key.json'
# key_path = '~/workspace/xunke00/coinbasepro/view_key.json'
# key_path = '~/workspace/xunke00/gopax/view_key.json'
# key_path = '~/workspace/prestoinvt00/Huobi/view_key.json'
# key_path = '~/workspace/prestoinvt00/Huobi/view_key.json'
# key_path = '~/workspace/xunke00/korbit/view_key.json'
# key_path = '~/workspace/xunke00/kraken/view_key.json'
# key_path = '~/workspace/xunke00/lmax_digital/view_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/xunke00/prex/view_key.json'
# key_path = '~/workspace/xunke09/upbit/view_key.json'
key_path = '~/workspace/prestoinv0/Deribit/view_key.json'

# query = {
# "mea": "Futures.Binance.v1",
# "mea": "Futures.Bitflyer.v1",
# "mea": "Futures.Bitmex.v1",
# "mea": "Futures.Bybit.v2",
# "mea": "Futures.Ftx.v1",
# "mea": "Futures.Huobi.v1",
# "mea": "Futures.Okex.v3",
# "mea": "Futures.Okex.v5",
# "mea": "Futures.Prex.v1",
# "type": "QUERY_ACCOUNT_POSITION"
# }

# key_path = '~/workspace/xunke00/binance_fut/view_key.json'
# key_path = '~/workspace/xunke05/bitflyer/view_key.json'
# key_path = '~/workspace/xunke00/bitmex/view_key.json'
# key_path = '~/workspace/xunke00/bybit/view_key.json'
# key_path = '~/workspace/xunke00/FTX/view_key.json'
# key_path = '~/workspace/prestoinvt00/Huobi/view_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/prestoinvt00/OKEx/view_v3_key.json'
# key_path = '~/workspace/xunke00/prex/view_key.json'

# key_path = '~/config/coin_key_high/trade/xunke18/binance/trade_key.json'
# key_path = '/home/xguo/config/coin_key_high/trade/xunke00/prex/trade_key.json'
key_path = os.path.expanduser(key_path)

# "rest_host": "api.sandbox.prex.sh",
conn_config = {
    "key_filepath": key_path
}


balance_res = py_trade_client.submit_rest_query(query, conn_config)
print(balance_res)

status_code = balance_res['http_context']['http_response']['status_code']
body = balance_res['http_context']['http_response']['body']
if status_code in (200, 401):
    # body was originally bytes in proto msg, and is base64 encoded in json.
    balance_res['http_context']['http_response']['body'] = \
        json.loads(base64.b64decode(body).decode())
print(json.dumps(balance_res, indent=2))


sys.exit(0)
# old interface
balance_res = py_trade_client.submit_rest_query(query, conn_config)
status_code = balance_res['http_context']['http_response']['status_code']
body = balance_res['http_context']['http_response']['body']
if status_code == 200:
    # body was originally bytes in proto msg, and is base64 encoded in json.
    balance_res['http_context']['http_response']['body'] = \
        json.loads(base64.b64decode(body).decode())
print(json.dumps(balance_res, indent=2))


# interface compatible with coin1
client = py_trade_client.PrivateClient(query['mea'], key_path)
res = client.query_account_balance()
print(json.dumps(res, indent=2, default=str))
